// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db=cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.action) {
    case 'patient_get': {
      return patient_get(event)
    }
    case 'ao_get': {
      return ao_get(event)
    }
    case 'ss_get': {
      return ss_get(event)
    }
    case 'bo_adult_get': {
      return bo_adult_get(event)
    }
    case 'bo_child_get': {
      return bo_child_get(event)
    }
    case 'sche_get': {
      return sche_get(event)
    }
    case 'user_get': {
      return user_get(event)
    }
    case 'patient_modify': {
      return patient_modify(event)
    }
    case 'ao_modify': {
      return ao_modify(event)
    }
    case 'ss_modify': {
      return ss_modify(event)
    }
    case 'bo_adult_modify': {
      return bo_adult_modify(event)
    }
    case 'bo_child_modify': {
      return bo_child_modify(event)
    }
    case 'sche_modify': {
      return sche_modify(event)
    }
    default: {
      return
    }
  }
}
 
async function patient_get(event) {
  var{id}=event
  return await db.collection("patient").where({
    _id:id
  }).get()
}

async function ao_get(event) {
  var{id}=event
  return await db.collection("ao").where({
    _id:id
  }).get()
}

async function ss_get(event) {
  var{id}=event
  return await db.collection("ss").where({
    _id:id
  }).get()
}

async function bo_adult_get(event) {
  var{id}=event
  return await db.collection("bo").where({
    _id:id
  }).get()
}

async function bo_child_get(event) {
  var{id}=event
  return await db.collection("bo").where({
    _id:id
  }).get()
}

async function sche_get(event) {
  var{id}=event
  return await db.collection("sche").where({
    _id:id
  }).get()
}

async function user_get(event) {
  var{id}=event
  return await db.collection("user").where({
    _openid:id
  }).get()
}



async function patient_modify(event) {
  var{patient_name,patient_bed,patient_sex,patient_age,patient_blood_type,patient_chief,patient_diag,patient_treat,patient_oper_list_name,patient_oper_list_time,patient_others,patient_otherchief,database_id}=event
  patient_oper_list_time.time=new Date(patient_oper_list_time.time)
  for(var i=0;i<patient_others.length;i++){
    patient_others[i].time=new Date(patient_others[i].time)
  }
  //将手术类型手术日期进行组合
  var patient_oper_list=[
    {'name':patient_oper_list_name,'time':new Date(patient_oper_list_time)}
  ]
  // patient_oper_list.push.apply(patient_oper_list, patient_others);
  patient_oper_list=[...patient_oper_list,...patient_others]
  //将主管医师进行组合
  var patient_chief=[
    patient_chief
  ]
  // patient_chief.push.apply(patient_chief, patient_otherchief);
  patient_chief=[...patient_chief,...patient_otherchief]
  return await db.collection('patient').doc(database_id).update({
    data:{
      patient_name:patient_name,
      patient_bed:patient_bed,
      patient_sex:patient_sex,
      patient_age:patient_age,
      patient_blood_type:patient_blood_type,
      patient_chief:patient_chief,
      patient_diag:patient_diag,
      patient_treat:patient_treat,
      // patient_in_time:new Date(),
      patient_oper_list:patient_oper_list,
      // patient_del:false
    }
  })
}

async function ao_modify(event) {
  var {patient_id,ao_WBC,ao_HB,ao_PLT,ao_TB,ao_ALT,ao_AST,ao_GGT,ao_ALB,ao_ALP,ao_FK浓度,ao_FK,ao_激素,ao_T管,ao_体温,ao_入量,ao_出量,ao_尿量,ao_desc,ao_others,database_id}=event
  var ao_index=[
    {'name':'WBC','value':ao_WBC},
    {'name':'HB','value':ao_HB},
    {'name':'PLT','value':ao_PLT},
    {'name':'TB','value':ao_TB},
    {'name':'ALT','value':ao_ALT},
    {'name':'AST','value':ao_AST},
    {'name':'GGT','value':ao_GGT},
    {'name':'ALB','value':ao_ALB,},
    {'name':'ALP','value':ao_ALP},
    {'name':'FK浓度','value':ao_FK浓度},
    {'name':'FK','value':ao_FK},
    {'name':'激素','value':ao_激素},
    {'name':'T管','value':ao_T管},
    {'name':'体温','value':ao_体温},
    {'name':'入量','value':ao_入量},
    {'name':'出量','value':ao_出量},
    {'name':'尿量','value':ao_尿量}
  ]
  //  ao_index.push.apply(ao_index, ao_others);
  ao_index=[...ao_index,...ao_others]
  return await db.collection('ao').doc(database_id).update({
    data:{
        // patient_id:patient_id,
        ao_index:ao_index,
        ao_desc:ao_desc,
        // ao_time:new Date()
    }
  })
}

async function ss_modify(event) {
  var {patient_id,ss_temper_top,ss_temper_down,ss_rate_top,ss_rate_down,ss_press_top,ss_press_down,ss_blood_oxygen,ss_mind,ss_vision,ss_touch,ss_knock,ss_hear,ss_examine,ss_yester_desc,database_id}=event
  return await db.collection('ss').doc(database_id).update({
    data:{
        // patient_id:patient_id,
        // ss_time:Date(),
        ss_feature:{
          'temper_top':ss_temper_top,
          'temper_down':ss_temper_down,
          'rate_top':ss_rate_top,
          'rate_down':ss_rate_down,
          'press_top':ss_press_top,
          'press_down':ss_press_down,
          'blood_oxygen':ss_blood_oxygen,
          'mind':ss_mind,
          'belly_feat':{
            'vision':ss_vision,
            'touch':ss_touch,
            'knock':ss_knock,
            'hear':ss_hear
          }
        },
        ss_examine:ss_examine,
        ss_yester_desc:ss_yester_desc
    }
  })
}

async function bo_adult_modify(event) {
  var {bo_name,bo_bed,bo_sex,bo_age,bo_height,bo_weight,bo_blood_type,bo_situation,bo_meld,bo_child,bo_diag,database_id}=event
  return await db.collection('bo').doc(database_id).update({
    data:{
        // bo_age_type:"成人",
        bo_name:bo_name,
        bo_bed:bo_bed,
        bo_sex:bo_sex,
        bo_age:bo_age,
        bo_height:bo_height,
        bo_weight:bo_weight,
        bo_blood_type:bo_blood_type,
        bo_situation:bo_situation,
        bo_meld:bo_meld,
        bo_child:bo_child,
        bo_diag:bo_diag,
        // bo_del:false
    }
  })
}

async function bo_child_modify(event) {
  var {bo_name,bo_bed,bo_sex,bo_age,bo_height,bo_weight,bo_blood_type,bo_situation,bo_peld,bo_child,bo_diag,database_id}=event
  return await db.collection('bo').doc(database_id).update({
    data:{
        // bo_age_type:"儿童",
        bo_name:bo_name,
        bo_bed:bo_bed,
        bo_sex:bo_sex,
        bo_age:bo_age,
        bo_height:bo_height,
        bo_weight:bo_weight,
        bo_blood_type:bo_blood_type,
        bo_situation:bo_situation,
        bo_peld:bo_peld,
        bo_child:bo_child,
        bo_diag:bo_diag,
        // bo_del:false
    }
  })
}

async function sche_modify(event) {
  var {sche_group,sche_chief,sche_deputy_chief,sche_attend_doc,sche_resi_doc,sche_rota_doc,sche_rest_time,sche_discuss_time,sche_pub_edu,sche_in_consul,sche_out_consul,sche_department,sche_desc,sche_month,
    // sche_morechief,sche_moredeputy,sche_moreattend,sche_moreresi,sche_morerota,sche_day_list,
    database_id}=event
  // //将主任医师进行组合
  // var sche_chief=[
  //   sche_chief
  // ]
  // sche_chief=[...sche_chief,...sche_morechief]
  // //将副主任医师进行组合
  // var sche_deputy_chief=[
  //   sche_deputy_chief
  // ]
  // sche_deputy_chief=[...sche_deputy_chief,...sche_moredeputy]
  // //将主治医师进行组合
  // var sche_attend_doc=[
  //   sche_attend_doc
  // ]
  // sche_attend_doc=[...sche_attend_doc,...sche_moreattend]
  // //将住院医师进行组合
  // var sche_resi_doc=[
  //   sche_resi_doc
  // ]
  // sche_resi_doc=[...sche_resi_doc,...sche_moreresi]
  // //将轮转医师进行组合
  // var sche_rota_doc=[
  //   sche_rota_doc
  // ]
  // sche_rota_doc=[...sche_rota_doc,...sche_morerota]

  return await db.collection('sche').doc(database_id).update({
    data:{
        sche_month:sche_month,
        sche_group:sche_group,
        sche_chief:sche_chief,
        sche_deputy_chief:sche_deputy_chief,
        sche_attend_doc:sche_attend_doc,
        sche_resi_doc:sche_resi_doc,
        sche_rota_doc:sche_rota_doc,
        sche_rest_time:sche_rest_time,
        sche_discuss_time:sche_discuss_time,
        sche_pub_edu:sche_pub_edu,
        sche_in_consul:sche_in_consul,
        sche_out_consul:sche_out_consul,
        sche_department:sche_department,
        sche_desc:sche_desc,
        // sche_day_list:sche_day_list
    }
  })
}